package com.xxxx.seckill.controller;


import com.alibaba.fastjson.JSON;
import com.xxxx.seckill.pojo.SeckillOrder;
import com.xxxx.seckill.rabbitmq.MQSender;
import com.xxxx.seckill.service.IOrderService;
import com.xxxx.seckill.utils.JsonUtil;
import com.xxxx.seckill.vo.OrderDeatilVo;
import com.xxxx.seckill.vo.RespBean;
import com.xxxx.seckill.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lulongfei
 * @since 2022-04-19
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    /**
     * 详情页面
     */
    @Autowired
    private IOrderService iOrderService;


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private MQSender mqSender;

    /**
     * 描述信息
     *
     * @param tUser   用户信息
     * @param orderId 订单主键
     * @return respBean
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    public RespBean detail(UserController tUser, Long orderId) {
        if (tUser == null) {
            return RespBean.error(RespBeanEnum.SESSION_ERROR);
        }
        OrderDeatilVo orderDeatilVo = iOrderService.detail(orderId);
        return RespBean.success(orderDeatilVo);
    }


    @RequestMapping(value = "/cancelOrder", method = RequestMethod.GET)
    @ResponseBody
    public RespBean cancelOrder(@RequestParam(value = "userId") String userId,
                                @RequestParam(value = "goodsId") Long goodsId) {
        if (ObjectUtils.isEmpty(userId) || ObjectUtils.isEmpty(goodsId)) {
            return RespBean.error(RespBeanEnum.SESSION_ERROR);
        }
        //支付的缓存
        if (redisTemplate.hasKey("pay:" + userId + ":" + goodsId)) {
            return RespBean.error(RespBeanEnum.ORDER_PAY_ERROR);
        }

        if (!redisTemplate.hasKey("order:" + userId + ":" + goodsId)) {
            return RespBean.error(RespBeanEnum.ORDER_NOT_EXIST);
        }
        SeckillOrder seckillOrder = (SeckillOrder) redisTemplate.opsForValue().get("order:" + userId + ":" + goodsId);

        // 订单信息，转换为str，并发送出去
        mqSender.sendCancelOderMessage(JSON.toJSONString(seckillOrder));

        return RespBean.success();


    }

    /**
     * 支付操作
     *
     * @param userId 用户主键
     * @param goodId 商品主键
     * @return RespBean
     */
    @RequestMapping(value = "/payOrder", method = RequestMethod.GET)
    @ResponseBody
    public RespBean payOrder(@RequestParam(value = "userId") String userId,
                             @RequestParam(value = "goodsId") Long goodId) {
        // 判断用户主键、商品主键是否为空
        if (ObjectUtils.isEmpty(userId) || ObjectUtils.isEmpty(goodId)) {
            return RespBean.error(RespBeanEnum.BIND_ERROR);
        }
        // 获取redis 的值
        if (!redisTemplate.hasKey("order:" + userId + ":" + goodId)) {
            return RespBean.error(RespBeanEnum.ORDER_NOT_EXIST);
        }
        final SeckillOrder seckillOrder = (SeckillOrder) redisTemplate.opsForValue().get("order:" + userId + ":" + goodId);
        // 订单信息，转换为str，并发送出去
        mqSender.sendPayOderMessage(JsonUtil.object2JsonStr(seckillOrder));
        return RespBean.success();
    }
}
